<template>
  <div style="margin: 20px">
    <el-card>
      <div style="margin-bottom: 10px; display: flex">
        <el-input class="w-50 m-2" style="width: 150px" placeholder="请输入关键词" v-model="form.keywords" />
        <el-button style="margin-left: 10px" type="primary" @click="get">关键词搜索</el-button>
        <el-button @click="toAdd">新建项目</el-button>
      </div>
      <el-table :data="state.tableData" :border="true" highlight-current-row>
        <el-table-column prop="id" label="ID" width="50" />
        <el-table-column prop="name" label="项目名称" width="150" />
        <el-table-column prop="description" label="项目描述" />
        <el-table-column prop="createTime" label="建立时间" width="160" />
        <el-table-column fixed="right" label="操作" width="150">
          <template v-slot="scope">
            <el-button link size="small" @click="toSolution(scope.row)">方案</el-button>
            <el-button link size="small" @click="toEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="是否确认删除该项？" confirm-button-text="是" cancel-button-text="否"
              @confirm="doDel(scope.row.id)">
              <template #reference>
                <el-button link type="danger" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="pagination" v-model:current-page="form.current" v-model:page-size="form.size"
        :page-sizes="[10, 20, 50]" :small="false" :disabled="false" :background="true"
        layout="total, sizes, prev, pager, next, jumper" :total="state.total" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>
    <el-dialog v-model="dlgState.visible" title="新增项目" :width="600" @close="clean">
      <el-form :model="dlgState.form" ref="dlg_form">
        <el-form-item label="项目名称：" prop="name">
          <el-input v-model="dlgState.form.name"></el-input>
        </el-form-item>
        <el-form-item label="项目描述：" prop="description">
          <el-input v-model="dlgState.form.description" type="textarea" :rows="3"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancelAdd">返回</el-button>
          <el-button type="primary" @click="doAdd" v-if="dlgState.isAdd">
            新增
          </el-button>
          <el-button type="primary" @click="doEdit" v-else> 保存 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
  <el-button @click="test"></el-button>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import router from "@/router";
//import projectApi from "@/api/infrasturcture/project";
import projectApi from "@/api/equipment/project";
import { PROJECT_TYPE } from "@/NAPI";

// type definition
type project = {
  id: number;
  name: string;
  type: number;
  createTime: Date;
  description: string;
};

// tableData
const form = reactive({
  current: 1,
  keywords: "",
  size: 10,
  project_type: 3,
});
const state = reactive({
  total: 0,
  tableData: [] as Array<project>,
});
const handleSizeChange = (val: number) => {
  form.size = val;
  get();
};
const handleCurrentChange = (val: number) => {
  form.current = val;
  get();
};
const get = () => {
  console.log("get");
  projectApi.get({ ...form, project_type: PROJECT_TYPE.PUR }).then((res) => {
    if (res.data.code === 200) {
      state.tableData = res.data.data.records;
      state.total = res.data.data.total;
    }
  });
};
// 对话框
const dlg_form = ref();
const dlgState = reactive({
  visible: false,
  isAdd: true,
  title: "新增项目",
  form: {
    id: 0,
    name: "",
    type: 1,
    description: "",
  },
});
const toAdd = () => {
  dlgState.isAdd = true;
  dlgState.title = "新增项目"
  dlgState.visible = true;
};
const cancelAdd = () => {
  dlgState.visible = false;
};
const doAdd = () => {
  projectApi.add({ ...dlgState.form, project_type: PROJECT_TYPE.PUR }).then((res) => {
    if (res.status === 200) {
      if (res.data.code === 500) {
        ElMessage.error("名称重复");
      } else {
        ElMessage.success("操作成功");
        dlgState.visible = false;
      }
      get();
    }
  });
};
const clean = () => {
  dlgState.form = {
    id: 0,
    name: "",
    type: 1,
    description: "",
  };
};

const toEdit = (row: project) => {
  dlgState.form = row;
  dlgState.isAdd = false;
  dlgState.title = "编辑项目"
  dlgState.visible = true;
  console.log(editForm.form);
};
const doEdit = () => {
  projectApi.update({ ...dlgState.form, project_type: PROJECT_TYPE.PUR }).then((res) => {
    console.log(res);
    if (res.status === 200) {
      if (res.data.code === 500) {
        ElMessage.error("名称重复");
      } else {
        ElMessage.success("保存成功");
        dlgState.visible = false;
      }
      get();
    }
  });
};

// 项目详情（方案视图）
const toSolution = (project: project) => {
  console.log(project);
  window.sessionStorage.setItem("project", JSON.stringify(project));
  console.log(window.sessionStorage.getItem("project"));
  router.push("equipment/solution");
};
// 项目删除
const doDel = (id: number) => {
  projectApi.del(id).then((res) => {
    if (res.status === 200) {
      ElMessage.success("删除成功");
      get();
    }
  });
};
// 项目编辑
const editForm = reactive({
  form: {
    name: "",
    type: 1,
    description: "",
  },
});

const test = () => {
  console.log(router.options.routes);
};

onMounted(() => {
  get();
});
</script>
<style lang="scss" scoped>
.tabs>.el-tabs__content {
  width: 100%;
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.tabs {
  margin: 20px;
}
</style>
